package com.yangxixnin.lesson.task;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.util.Random;
import java.util.concurrent.Future;

/** @Author yangxinxin
 * @Description //TODO
 * @Date 16:52 2020/2/28
 * @Param
 * @return
 **/
@Component
public class AsyncTask {

    public  static Random  random = new Random();
    @Async("myExecutor")
    public Future<String > doTaskOne() throws Exception {
        System.out.println("开始任务一");
        long startTiems = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long endTimes = System.currentTimeMillis();
        System.out.println("完成任务一消耗的时间："+(endTimes - startTiems)+"毫秒");
        return  new AsyncResult<>("完成任务一");
    }
    @Async("myExecutor")
    public Future<String >  doTaskTwo() throws Exception {
        System.out.println("开始任务二");
        long startTiems = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long endTimes = System.currentTimeMillis();
        System.out.println("完成任务二消耗的时间："+(endTimes - startTiems)+"毫秒");
        return  new AsyncResult<>("完成任务二");
    }
    @Async("myExecutor")
    public Future<String >   doTaskThreee() throws Exception {
        System.out.println("开始任务三");
        long startTiems = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long endTimes = System.currentTimeMillis();
        System.out.println("完成任务三消耗的时间："+(endTimes - startTiems)+"毫秒");
        return  new AsyncResult<>("完成任务三");
    }

}
